package service.impl;

import models.Category;
import play.db.jpa.JPA;
import service.CategoryInterface;

import javax.persistence.Query;
import java.util.List;

public class CategoryImpl implements CategoryInterface {
    @Override
    public List<Category> getCategoryListByPage(Integer parentId, Integer pageSize, Integer pageNum) {
        StringBuffer sb = new StringBuffer();
        sb.append("from Category c where 1=1 ");
        if(parentId!=null && parentId>0){
            sb.append(" and c.parent_id=? ");
        }else{
            sb.append(" and c.parent_id is null ");
        }
        Query query = JPA.em().createQuery(sb.toString(), Category.class);
        if(parentId!=null && parentId>0){
            query.setParameter(1, parentId);
        }
        query.setFirstResult((pageNum - 1) * pageSize);
        query.setMaxResults(pageSize);
        return query.getResultList();
    }
}
